/* eslint-disable no-extra-parens */
import { InitModalMethod, ParseModalMethod } from './defination';

/**
 * 设置表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const initModalData = (form = {}, workstep = {}, tts = false) => {
	if (!form || !workstep || !workstep.do) {
		return;
	}

	form.aiSpeak = workstep.do?.data; // 1
	form.aiSpeakVideo = workstep.do?.video;
	form.waitTime = workstep.after.data?.speechBeginWaitingTimeMS; // 2

	// other
	if (workstep.answer[0]?.after) {
		form.wordsOther = workstep.answer[0]?.do?.data; // 3
		form.wordsOtherVideo = workstep.answer[0]?.do?.video;
		form.wordsOverTime = workstep.answer[0]?.after?.data?.speechBeginWaitingTimeMS; // 4
	} else {
		form.wordsOverTime = workstep.answer[0]?.do?.data?.speechBeginWaitingTimeMS; // 4
		form.wordsOtherVideo = workstep.answer[0]?.do?.video;
	}

	// 其他回答多次后
	if (workstep.errorRemind?.after) {
		form.wordsEnd = workstep.errorRemind?.do?.data; // 6
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
		form.nextStepOther = workstep.errorRemind?.after?.id;
	} else {
		form.nextStepOther = workstep.errorRemind?.do?.id;
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
	}
	form.otherTimesMax = workstep.errorRemind?.maximum; // 5

	// noAnswer
	if (workstep.answer[1]?.after) {
		form.wordsNoAnswer = workstep.answer[1]?.do?.data; // 7
		form.wordsNoAnswerVideo = workstep.answer[1]?.do?.video;
		form.noAnswerOverTime = workstep.answer[1]?.after?.data?.speechBeginWaitingTimeMS; // 4
	} else {
		form.noAnswerOverTime = workstep.answer[1]?.do?.data?.speechBeginWaitingTimeMS; // 4
		form.wordsNoAnswerVideo = workstep.answer[1]?.do?.video;
	}

	// 不回答多次后
	if (workstep.emptyErrorRemind?.after) {
		form.nextStepNoAnswer = workstep.emptyErrorRemind?.after?.id;
		form.wordsEndNoAnswer = workstep.emptyErrorRemind?.do?.data; // 10
		form.wordsEndNoAnswerVideo = workstep.emptyErrorRemind?.do?.video;
	} else {
		form.nextStepNoAnswer = workstep.emptyErrorRemind?.do?.id;
		form.wordsEndNoAnswerVideo = workstep.emptyErrorRemind?.do?.video;
	}

	form.noAnswerTimesMax = workstep.emptyErrorRemind?.maximum; // 9
	// 识别失败下一步
	if (workstep.recognitionFailureRemind?.after) {
		form.llmRecognizeNextStep = workstep.recognitionFailureRemind?.after?.id;
	} else {
		form.llmRecognizeNextStep = workstep.recognitionFailureRemind?.do?.id;
	}

	form.llmRecognizeTimesMax = workstep.recognitionFailureRemind?.maximum;
	// Intent point 意图点
	form.intentPoints = [];
	workstep.answer.map(item => {
		if (item.type.includes('intent_point')) {
			let option = {};
			option.answerIntention = item.key; // 意图点
			option.answerSpeak = item.after ? item.do?.data : ''; // ai说
			option.video = item.after ? item.do.video : tts ? item.do.video : null; // video模式视频
			option.nextStepsAnswer = item.after ? item.after.id : item.do.id; // 下一步
			option.additionalInformation = item.additionalInformation || []; // 待补充信息绑定的变量
			option.answerSpeakVariables = item.after ? item.do.variables : tts ? item.do.variables : [];
			form.intentPoints.push(option);
		}
	});

	/** 变量存储处理开始 */
	form['aiSpeakVariableId'] = workstep.do.variableId;
	form['aiSpeakVariables'] = workstep.do.variables;
	form['wordsOtherVariableId'] = workstep.answer[0].do.variableId;
	form['wordsOtherVariables'] = workstep.answer[0].do.variables?.filter(item => form['wordsOther']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsNoAnswerVariableId'] = workstep.answer[1].do.variableId;
	form['wordsNoAnswerVariables'] = workstep.answer[1].do.variables.filter(item => form['wordsNoAnswer']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsEndVariableId'] = workstep.errorRemind.do.variableId;
	form['wordsEndVariables'] = workstep.errorRemind.do.variables?.filter(item => form['wordsEnd']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsEndNoAnswerVariableId'] = workstep.emptyErrorRemind.do.variableId;
	form['wordsEndNoAnswerVariables'] = workstep.emptyErrorRemind.do.variables?.filter(item => form['wordsEndNoAnswer'].indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */

	form.name = workstep.name;
};
InitModalMethod['llm'] = initModalData;

/**
 * 解析表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const parseModalData = (form = {}, workstep = {}, tts = false) => {
	console.log('llm parseModalData', form, workstep);
	if (!form || !workstep || !workstep.do) {
		return;
	}

	workstep.do.data = form.aiSpeak; // 1
	workstep.do.video = form.aiSpeakVideo;

	workstep.after.data.speechBeginWaitingTimeMS = form.waitTime; // 2

	workstep.answer = [
		{
			do: {
				data: '',
				command: 'speak',
				variableId: '',
				variables: []
			},
			key: 'others',
			type: 'other',
			after: {
				data: {
					speechBeginWaitingTimeMS: 3000
				},
				command: 'listen'
			}
		},
		{
			key: 'empty',
			do: {
				data: '',
				command: 'speak',
				variableId: '',
				variables: []
			},
			after: {
				data: {
					speechBeginWaitingTimeMS: 3000
				},
				command: 'listen'
			},
			type: 'empty'
		}
	];
	// other
	if ((form.wordsOther && !tts) || (form.wordsOtherVideo && !tts) || (tts && form.wordsOther && form.wordsOtherVideo)) {
		workstep.answer[0].do.data = form.wordsOther; // 4
		workstep.answer[0].do.video = form.wordsOtherVideo;
		workstep.answer[0].after.data.speechBeginWaitingTimeMS = form.wordsOverTime; // 4
	} else {
		workstep.answer[0].do = Object.assign({}, workstep.answer[0].after);
		workstep.answer[0].do.data.speechBeginWaitingTimeMS = form.wordsOverTime; // 4
		workstep.answer[0].do.video = form.wordsOtherVideo;
		delete workstep.answer[0].after;
	}
	if ((form.wordsEnd && !tts) || (form.wordsEndVideo && !tts) || (tts && form.wordsEnd && form.wordsEndVideo)) {
		workstep.errorRemind.after.id = form.nextStepOther;
		workstep.errorRemind.do.data = form.wordsEnd; // 5
		workstep.errorRemind.do.video = form.wordsEndVideo;
	} else {
		workstep.errorRemind.do = Object.assign({}, workstep.errorRemind.after);
		workstep.errorRemind.do.id = form.nextStepOther;
		workstep.errorRemind.do.video = form.wordsEndVideo;
		delete workstep.errorRemind.after;
	}
	workstep.errorRemind.maximum = form.otherTimesMax; // 5

	// noAnswer
	if ((form.wordsNoAnswer && !tts) || (form.wordsNoAnswerVideo && !tts) || (tts && form.wordsNoAnswer && form.wordsNoAnswerVideo)) {
		workstep.answer[1].do.data = form.wordsNoAnswer; // 7
		workstep.answer[1].do.video = form.wordsNoAnswerVideo;
		workstep.answer[1].after.data.speechBeginWaitingTimeMS = form.noAnswerOverTime; // 4
	} else {
		workstep.answer[1].do = Object.assign({}, workstep.answer[1].after);
		workstep.answer[1].do.data.speechBeginWaitingTimeMS = form.noAnswerOverTime; // 4
		workstep.answer[1].do.video = form.wordsNoAnswerVideo;
		delete workstep.answer[1].after;
	}

	if ((form.wordsEndNoAnswer && !tts) || (form.wordsEndNoAnswerVideo && !tts)) {
		workstep.emptyErrorRemind.after.id = form.nextStepNoAnswer;
		workstep.emptyErrorRemind.do.data = form.wordsEndNoAnswer; // 10
		workstep.emptyErrorRemind.do.video = form.wordsEndNoAnswerVideo;
	} else {
		workstep.emptyErrorRemind.do = Object.assign({}, workstep.emptyErrorRemind.after);
		workstep.emptyErrorRemind.do.id = form.nextStepNoAnswer;
		workstep.emptyErrorRemind.do.video = form.wordsEndNoAnswerVideo;
		delete workstep.emptyErrorRemind.after;
	}

	workstep.emptyErrorRemind.maximum = form.noAnswerTimesMax; // 9

	// 识别失败
	workstep.recognitionFailureRemind.maximum = form.llmRecognizeTimesMax; // 5
	// 识别失败下一步
	workstep.recognitionFailureRemind.do.id = form.llmRecognizeNextStep;

	let index = 0;
	console.log('###form.intentPoints', form.intentPoints);

	form.intentPoints.map(item => {
		index++;
		let option = { type: `intent_point${index}`, key: item.answerIntention, additionalInformation: item.additionalInformation };
		console.log('###@@@', item, tts);
		item.answerSpeak = item.answerSpeak.replace(/\n/g, '');
		if ((item.answerSpeak && !tts) || (item.video && !tts) || (tts && item.answerSpeak && item.video)) {
			option.do = { data: item.answerSpeak, command: 'speak', variables: [] };
			option.do.video = item.video;
			option.after = { command: 'jump-to', id: item.nextStepsAnswer, no: item.nextStepsAnswer };
		} else {
			option.do = { command: 'jump-to', id: item.nextStepsAnswer, no: item.nextStepsAnswer };
			option.do.video = item.video;
		}
		workstep.answer.push(option);
	});

	/** 变量存储处理开始 */
	workstep.do.variableId = form['aiSpeakVariableId'];
	workstep.do.variables = form['aiSpeakVariables'];

	workstep.answer[0].do.variableId = form['wordsOtherVariableId'];
	workstep.answer[0].do.variables = form['wordsOtherVariables']?.filter(item => form['wordsOther'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[1].do.variableId = form['wordsNoAnswerVariableId'];
	workstep.answer[1].do.variables = form['wordsNoAnswerVariables']?.filter(item => form['wordsNoAnswer'].indexOf(`{{${item.name}}}`) !== -1);
	workstep.errorRemind.do.variableId = form['wordsEndVariableId'];
	workstep.errorRemind.do.variables = form['wordsEndVariables']?.filter(item => form['wordsEnd'].indexOf(`{{${item.name}}}`) !== -1);
	workstep.emptyErrorRemind.do.variableId = form['wordsEndNoAnswerVariableId'];
	workstep.emptyErrorRemind.do.variables = form['wordsEndNoAnswerVariables']?.filter(item => form['wordsEndNoAnswer'].indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */
	workstep.name = form.name;
};
ParseModalMethod['llm'] = parseModalData;

/**
 * {
	id: '',
	no: 'llmIntentRecognition_4',
	name: '',
	type: 'llmIntentRecognition',
	do: {
		data: '',
		video: null,
		command: 'speak',
		variables: [],
		variableId: null
	},
	after: {
		data: {
			speechBeginWaitingTimeMS: 3000
		},
		command: 'listen'
	},
	answer: [
		{
			do: {
				data: '', // 其他回答
				video: null,
				command: 'speak',
				variables: [],
				variableId: null
			},
			key: 'others',
			type: 'other',
			after: {
				data: {
					speechBeginWaitingTimeMS: 3000
				},
				command: 'listen'
			}
		},
		{
			do: {
				data: '', // 不回答
				video: null,
				command: 'speak',
				variables: [],
				variableId: null
			},
			key: 'empty',
			type: 'empty',
			after: {
				data: {
					speechBeginWaitingTimeMS: 3000
				},
				command: 'listen'
			}
		},
		// 意图点分支， 可能多个，最多6个
		{
			do: {
				data: '',
				video: '',
				command: 'speak',
				variables: []
			},
			key: 'insurance_tickets', // 意图点内容，如意图点1:姓名name
			type: 'intent_point',
			additionalInformation: [
				{
					do: {
						data: '' // 要绑定的变量
					},
					// 具体变量详情
					additionalInfovariable: {
						// id: 24,
						// url: '',
						// name: '性别',
						// type: 'text',
						// method: '',
						// params: [],
						// roleId: 4,
						// initMode: 'user-data',
						// linkedKey: 'gender',
						// dataMasking: {}
					}
				}
			],
			after: {
				id: '',
				no: '',
				command: 'jump-to'
			}
		}
	],
	errorRemind: {
		do: {
			data: '', // 其他回答
			video: null,
			command: 'speak',
			variables: [],
			variableId: null
		},
		type: 'other',
		after: {
			id: '',
			no: '',
			command: 'jump-to'
		},
		maximum: 0
	},
	emptyErrorRemind: {
		do: {
			data: '', // 不回答
			video: null,
			command: 'speak',
			variables: [],
			variableId: null
		},
		type: 'empty',
		after: {
			id: '',
			no: '',
			command: 'jump-to'
		},
		maximum: 0
	},
	recognitionFailureRemind: {
		// do: {
		// 	data: '', // 识别失败
		// 	video: null,
		// 	command: 'speak',
		// 	variables: [],
		// 	variableId: null
		// },
		do: {
			id: '',
			no: '',
			command: 'jump-to'
		},
		maximum: 0,
		type: 'unrecognized'
	}
};
 */
